package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.common.util.PropertyPhase;
import ipebg.mzk.facade.entity.MsAdminUser;
import ipebg.mzk.facade.system.bean.LoginBean;
import ipebg.mzk.facade.system.bean.UserDTO;
import ipebg.mzk.facade.system.service.LoginService;
import ipebg.mzk.facade.system.service.UserDTOService;
import ipebg.mzk.provider.system.dao.AdminUserDao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("loginServiceImpl")
public class LoginServiceImpl  implements LoginService {
	private static Log log = LogFactory.getLog(OrgLevelServiceImpl.class);
	
	@Resource(name = "userDTOServiceImpl")
	UserDTOService userDTOServiceImpl;

	@Resource(name = "adminUserDao")
	AdminUserDao adminUserDao;

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public MsAdminUser getUserByUserName(String username) throws Exception {
		Session session = null;
		String hql = null;
		Query query = null;
		List<MsAdminUser> userList = null;
		MsAdminUser adminuser = null;
		try {
			hql = "from MsAdminUser u where u.enabled='" + 1 + "'and u.loginName = '" + username + "'";
			userList = adminUserDao.queryForList(hql, null);
			if (userList != null && userList.size() == 1) {
				adminuser = userList.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Get user base info from MS_USER table failed." + e.getMessage());
			throw e;
		} finally {
			hql = null;
			query = null;
		}

		// 使用一賬通數據，刷新用戶郵箱地址
		String START_USER_DTO = PropertyPhase.readData("START_USER_DTO");
		if (START_USER_DTO != null && START_USER_DTO.equalsIgnoreCase("TRUE")) {
			UserDTO userDTO;
			try {
				userDTO = userDTOServiceImpl.getUserDTOByAccountName(username);
				if (adminuser != null && userDTO != null) {
					adminuser.setPassword(userDTO.getPassword());
					if (userDTO.getEmail() != null && userDTO.getEmail() != "" && adminuser.getEmail().indexOf("@") < 1) {
						adminuser.setEmail(userDTO.getEmail());
						adminUserDao.update(adminuser);
					}
				} else {
					System.out.println("User :" + username + " 未申請一賬通賬號");
					log.info("User :" + username + " 未申請一賬通賬號");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				log.error("Get user info from DTO error," + e.getMessage());
				throw e;
			}
		}
		return adminuser;
	}

	@Override
	public boolean userLogin(LoginBean loginBean) {
		boolean flag = false;
		try {
			List<MsAdminUser> list=adminUserDao.findByProperty(MsAdminUser.class, new String[]{"loginName","password"}, new String[]{ loginBean.getUsername(),loginBean.getPassword()});
			if (list!= null) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return flag;
	}
}
